<?php
#注册插件
RegisterPlugin("sf_cache","ActivePlugin_sf_cache");

function ActivePlugin_sf_cache() {
	Add_Filter_Plugin('Filter_Plugin_Index_Begin','sf_cache_begin');	
	Add_Filter_Plugin('Filter_Plugin_Index_End','sf_cache_end');
	Add_Filter_Plugin('Filter_Plugin_PostArticle_Succeed','sf_cache_postArticle');
}
function InstallPlugin_sf_cache() {
	global $zbp;
	$zbp->Config('sf_cache')->diskenable=1;
	if(!$zbp->Config('sf_cache')->HasKey('disktime')) {
		$zbp->Config('sf_cache')->disktime=10;
	}
	if(!$zbp->Config('sf_cache')->HasKey('clearCacheAfterPostArticle')) {
		$zbp->Config('sf_cache')->clearCacheAfterPostArticle=0;
	}
	$zbp->SaveConfig('sf_cache');
}
function UninstallPlugin_sf_cache() {
	global $blogpath;
	sf_cache_deleteDir($blogpath .'/zb_users/cache/sf_cache/');
}

function sf_cache_postArticle($article){
	global $zbp;
	global $blogpath;
	if($zbp->Config('sf_cache')->HasKey('clearCacheAfterPostArticle')) {
		if($zbp->Config('sf_cache')->clearCacheAfterPostArticle==1){
			sf_cache_deleteDir($blogpath .'/zb_users/cache/sf_cache/');
		}
	}
}
function sf_cache_deleteDir($dirName){ 
	if(is_dir($dirName)){
		if ( $handle = opendir( "$dirName" ) ) {  
			while ( false !== ( $item = readdir( $handle ) ) ) {  
				if ( $item != "." && $item != ".." ) {  
					if ( is_dir( "$dirName/$item" ) ) {  
						sf_cache_deleteDir( "$dirName/$item" );  
					} else {  
						unlink( "$dirName/$item" );
					}
				}  
			}  
	   }  
		closedir( $handle );  
		rmdir( $dirName );
	}
}

function sf_cache_request_uri2(){
	if (isset($_SERVER['HTTP_X_REWRITE_URL'])) { // check this first so IIS will catch  
		$requestUri = $_SERVER['HTTP_X_REWRITE_URL'];  
    } elseif (  
        // IIS7 with URL Rewrite: make sure we get the unencoded url (double slash problem)  
        isset($_SERVER['IIS_WasUrlRewritten'])  
        && $_SERVER['IIS_WasUrlRewritten'] == '1'  
        && isset($_SERVER['UNENCODED_URL'])  
        && $_SERVER['UNENCODED_URL'] != ''  
        ) {  
		    $requestUri = $_SERVER['UNENCODED_URL'];  
    } elseif (isset($_SERVER['REQUEST_URI'])) {  
		$requestUri = $_SERVER['REQUEST_URI'];  
        // Http proxy reqs setup request uri with scheme and host [and port] + the url path, only use url path  
        #$schemeAndHttpHost = $this->getScheme() . '://' . $this->getHttpHost();  
        #if (strpos($requestUri, $schemeAndHttpHost) === 0) {  
		#	$requestUri = substr($requestUri, strlen($schemeAndHttpHost));  
        #}  
    } elseif (isset($_SERVER['ORIG_PATH_INFO'])) { // IIS 5.0, PHP as CGI  
        $requestUri = $_SERVER['ORIG_PATH_INFO'];  
        if (!empty($_SERVER['QUERY_STRING'])) {  
            $requestUri .= '?' . $_SERVER['QUERY_STRING'];  
        }  
    } else {    
    } 
	return $requestUri;
}

function sf_cache_begin(){
	global $zbp;

	if(!$zbp->Config('sf_cache')->diskenable)return;
	$dir =$zbp->path .'/zb_users/cache/sf_cache/';
	if(!is_dir($dir)){
		mkdir ($dir);
	}
	$sf_cache_key=$dir.md5(sf_cache_request_uri2()).'.dat';
	if(file_exists($sf_cache_key)){
		$sf_cache_file_time=filemtime($sf_cache_key);
		if($sf_cache_file_time){
			$nowtime = time();
			$disktime=$zbp->Config('sf_cache')->disktime;
			if(($nowtime-$sf_cache_file_time)<($disktime*60)){
				echo file_get_contents($sf_cache_key);
				echo "<!-- 读取缓存文件 ：".date('Y-m-d H:i:s',$sf_cache_file_time).",缓存时间：".$disktime."分钟 -->";
				RunTime();
				exit;
			}
		}
	}
	ob_start();
}

function sf_cache_end(){
	global $zbp;
	if(!$zbp->Config('sf_cache')->diskenable)return;
/* 
	if(isset($zbp->user) && $zbp->user->Level == 1){ */
		$sf_cache=ob_get_contents();
		$dir =$zbp->path .'/zb_users/cache/sf_cache/';
		$sf_cache_key=$dir.md5(sf_cache_request_uri2()).'.dat';
		$fp=fopen($sf_cache_key,'w+');
		fwrite($fp,$sf_cache);//写缓存ob_get_contents()到$dir内的文件
		fclose($fp);
		ob_end_flush();
/* 	}else{
		ob_end_flush();
	} */

}